<?php
declare (strict_types = 1);

namespace app\command;

use app\crm\model\GzhSendLog;
use app\mini\model\SendUrlModel;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\facade\Session;

class GzhSend extends Command
{
    /**
     * 发送公众号，需要时执行
     *
     * @return void
     */
    protected function configure()
    {
        // 指令配置
        $this->setName('gzhsend')
            ->setDescription('the gzhsend command');
    }

    protected function execute(Input $input, Output $output)
    {
        // 指令输出
        $config = [
            'xl3Z_wkofGfGtgad2UkIKLktqyh8jYqNjToHvNTYetM'=>array()
        ];
        $list = GzhSendLog::where('status', 0)->order('id', 'asc')->select()->toArray();
        if(Session::has('gzh_access_token'))
        {
            $access_token=Session::get('gzh_access_token');
        }else{
            $model = new SendUrlModel();

            $url = "https://api.weixin.qq.com/cgi-bin/token";

            $jsonData = [

                'grant_type' => 'client_credential',

                'appid'      => 'wx7cde54d2bb9607e6',

                'secret'     => '1aa86183eca8d6ebf854cbc0349543df',

            ];

            $data = $model->send_url($url,$jsonData);

            $fomateToken = json_decode($data,true);


            $access_token = $fomateToken['access_token'];
            if(!$access_token)
            {
                return [
                    'code'=>400001,
                    'msg'=>"获取token失败"
                ];
            }
            Session::set('gzh_access_token',$access_token);
        }



        $posturl = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='.$access_token;

        $dt = array(
            "touser" => 'orXbIs5Il9x5cC9wT_kP-GzS29Wk',
            'template_id'  => 'TxPFSBEQ2y7868ZmaL7T8GAv12I5QKh4lvhctMgW0gc',
            'url'   => "",
            'data' => array(
                'thing5'       => array('value' => '大学路店'),
                'thing3'       => array('value' => '无针水光'),
                'short_thing4' => array('value' => '1'),
                'number9'      => array('value' => '3'),
                'time11'       => array('value' => date('Y-m-d H:i:s')),
            ),
        );
        foreach ($list as $value)
        {
//            $dt = array(
//                "touser" => 'orXbIs9RDp16tEveSqcWIYB82R6c',
//                'template_id'  => 'TxPFSBEQ2y7868ZmaL7T8GAv12I5QKh4lvhctMgW0gc',
//                'url'   => "",
//                'data' => $config[$value['template_id']]
//            );
            $postjson = json_encode($dt);
            $resder = $this->http_curl($posturl,'post','json',$postjson);
            print_r($resder);
        }


//        foreach ($list as $key =>$value)
//        {
//            $list[$key]['status']=1;
//        }
//        $user = new GzhSendLog;
//
//        $user->saveAll($list);

    }
    public function http_curl($url,$type,$res,$arr)
    {
        /**
         * $url 请求的url
         * $type 请求类型
         * $res 返回数据类型
         * $arr post请求参数
         */
        $ch=curl_init();

        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        if($type=='post'){
            curl_setopt($ch,CURLOPT_POST,1);
            curl_setopt($ch,CURLOPT_POSTFIELDS,$arr);
        }
        $output = curl_exec($ch);
        curl_close($ch);
        if($res=='json'){
            return json_decode($output,true);
        }

    }
}
